Ext.onReady(function(){ 

Ext.QuickTips.init();
Ext.form.Field.prototype.msgTarget = 'under';

var sm = new Ext.grid.CheckboxSelectionModel();
var cm=new Ext.grid.ColumnModel([ 
sm,
new Ext.grid.RowNumberer({header: "编号",width:40}),
{header:'债项ID',dataIndex:'debtNo',hidden:true},
{header:'客户ID',dataIndex:'custId',hidden:true},
{header:'客户名称',dataIndex:'custName',hidden:true},
{header:'债项名称',dataIndex:'debtDesc',width:100},
{header:'债项业务品种',dataIndex:'debtType',width:200},
{header:'合同金额（元）',dataIndex:'debtSum',width:100},
{header:'信贷余额（元）',dataIndex:'debtBalance',width:100},
{header:'币种',dataIndex:'currency',width:100},
{header:'起始日',dataIndex:'startDate',width:100},
{header:'到期日',dataIndex:'endDate',width:100},
{header:'当前资产分类',dataIndex:'classifyResult',width:100},
{header:'分类认定日期',dataIndex:'classifyDate',width:100}
]); 
var ds=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'obligation/listDebt.do',
method:'POST'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'debtNo'}, 
{name:'custId'}, 
{name:'custName'}, 
{name:'debtDesc'}, 
{name:'debtType'}, 
{name:'debtSum'}, 
{name:'debtBalance'}, 
{name:'currency'}, 
{name:'startDate'}, 
{name:'endDate'}, 
{name:'classifyResult'},
{name:'classifyDate'}
]) 
}); 
ds.load({params:{start:0,limit:20}}); 

var addCurrencyStore=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showType.do?type=Currency',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'name'}, 
{name:'code'}
])  ,
autoLoad:true
}); 

var addBusinessKindStore=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showType.do?type=BusinessKind',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'name'}, 
{name:'code'}
])  ,
autoLoad:true
}); 

var addGuarantyKindStore=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showType.do?type=GuarantyKind',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'name'}, 
{name:'code'}
])  ,
autoLoad:true
}); 

var addGroupLoanTypeStore=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showType.do?type=YesNo',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'name'}, 
{name:'code'}
])  ,
autoLoad:true
}); 

var addInterestCycleStore=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showType.do?type=GuarantyKind',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'name'}, 
{name:'code'}
])  ,
autoLoad:true
}); 

var addPInterestTypeStore=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showType.do?type=GuarantyKind',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'name'}, 
{name:'code'}
])  ,
autoLoad:true
}); 

var addNationalityStore=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showType.do?type=CountryCode',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'name'}, 
{name:'code'}
])  ,
autoLoad:true
}); 

var grid=new Ext.grid.GridPanel({ 
id:'myCustomers',
renderTo:"debt_list", 
stripeRows:true,//斑马线效果
title:"债项列表", 
height:500, 
cm:cm,
store: ds, 
loadMask: { msg: "加载中..." }, 
viewConfig: {   
                            forceFit:true   
            },   
            bbar: new Ext.PagingToolbar({   
                pageSize: 20,   
                store: ds,   
                displayInfo: true,   
                displayMsg: '第{0} 到 {1} 条数据 共{2}条',   
                emptyMsg: "没有数据"   
            }),
 tbar: [
 {text:'新增',id: 'btnAdd'},
 '-',
 {text:'查看详情',handler:viewDetail},
 '-',
 {text:'删除',handler:deleteCustomer}
 ]              
});

//新增客户表单
addForm = new Ext.form.FormPanel({ 
id: 'addCustomerForm', 
width: 650, 
height: 300, 
//样式 
bodyStyle: 'margin:3px 3px 3px 3px', 
frame: true, 
xtype: 'filedset', 
labelWidth: 100, 
items: 
[ { xtype: 'fieldset', 
title: '新增债项', 
collapsible: true, 
autoHeight: true, 
autoWidth: true, 
items: 
[ 

{
	layout:"column",
items:[{
	columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
	           items : [	{ 
	xtype: 'textfield', 
	id:'debtDesc',
	name: 'debtDesc', 
	fieldLabel: '债项名称', 
	emptyText: '必填', 
	blankText: '债项名称不能为空', 
	allowBlank: false, 
	maxLength: 10, 
	maxLengthText: '债项名称不能超过10个字符', 
	anchor: '98%' 
	}]},{ columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
	           items : [
	{
	xtype: 'combo', 
 	id : "debtType",
              store :addBusinessKindStore,
              fieldLabel: '债项业务品种',
              valueField : 'code',
              displayField : 'name',
              typeAhead : true,
              width: 120,
              mode:'remote', 
              emptyText : '请选择债项业务品种...',
              editable: false,
              triggerAction : 'all',
              selectOnFocus : true,
              hiddenName:'debtType',
              
	}]} 
]}, 
	
{
	layout:"column",
items:[{
	columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
		           items : [	{ 
	xtype: 'textfield', 
	id: 'debtSum',
	name: 'debtSum', 
	fieldLabel: '合同金额', 
	emptyText: '必填', 
	blankText: '债项金额不能为空', 
	allowBlank: false,  
	regex:/^[0-9]+\.{0,1}[0-9]{0,2}$/,
 	regexText:'只能输入整数或小数！' ,
	anchor: '98%' 
	}]},
	{ columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
	           items : [
	{
	xtype: 'combo', 
 	id : "currency",
              store :addCurrencyStore,
              fieldLabel: '币种',
              valueField : 'code',
              displayField : 'name',
              typeAhead : true,
              width: 120,
              mode:'remote', 
              emptyText : '请选择币种...',
              editable: false,
              triggerAction : 'all',
              selectOnFocus : true,
              hiddenName:'currency',
              
	}]} 
]}, 
	
{
	layout:"column",
	items:[{
		columnWidth : .5, // 该列有整行中所占百分比
        layout : "form",
        items : [{
           xtype: 'datefield',
           id: 'startDate', 
           name: 'startDate', 
           fieldLabel: '起始日',
           allowBlank: true,
           editable:false,  
           format : 'Y-m-d' ,
           width: 120
        }]
     },{
    	columnWidth : .5, // 该列有整行中所占百分比
        layout : "form",
        items : [{
           xtype: 'datefield',
			id: 'endDate', 
			name: 'endDate', 
			fieldLabel: '到期日',
			allowBlank: true,
			editable:false,  
			format : 'Y-m-d' ,
			width: 120
        }]
     }]
},

{
	layout:"column",
	items:[{
		columnWidth : .5, // 该列有整行中所占百分比
        layout : "form",
        items : [{
        	xtype: 'combo', 
        	id : "businessKind",
            store :addBusinessKindStore,
            fieldLabel: '授信形式',
            valueField : 'code',
            displayField : 'name',
            typeAhead : true,
            width: 120,
            mode:'remote', 
            emptyText : '请选择授信形式...',
            editable: false,
            triggerAction : 'all',
            selectOnFocus : true,
            hiddenName:'businessKind',
        }]
     },{ 
    	 columnWidth : .5, // 该列有整行中所占百分比
         layout : "form",
         items : [{
        	 xtype: 'combo', 
        	 id : "guarantyKind",
             store :addGuarantyKindStore,
             fieldLabel: '担保类型',
              valueField : 'code',
              displayField : 'name',
              emptyText : '请选择担保类型...',
              editable: false,
              typeAhead : true,
              width: 120,
              mode:'remote',  
              triggerAction : 'all',
              selectOnFocus : true,
              hiddenName:'guarantyKind'
         }]
     }]
}, 

{ layout : "form",
           items : [{
xtype: 'combo', 
 id : "groupLoanType",
              store :addGroupLoanTypeStore,
              fieldLabel: '是否是银团贷款',
              valueField : 'code',
              displayField : 'name',
              typeAhead : true,
              width: 120,
              mode:'remote', 
              emptyText : '请选择是否为银团贷款...',
              editable: false,
              triggerAction : 'all',
              selectOnFocus : true,
              hiddenName:'groupLoanType',
              
}]},

{
	layout:"column",
items:[{
	columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [
        	   { 
				xtype: 'textfield', 
				id: 'interestSum',
				name: 'interestSum', 
				fieldLabel: '利息总额',
				regex:/^[0-9]+\.{0,1}[0-9]{0,2}$/,
 				regexText:'只能输入整数或小数！' ,
				anchor: '98%' 
				}
           ]},
           
           { columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [
        	 {
		xtype: 'combo', 
		 id : "interestCycle",
              store :addInterestCycleStore,
              fieldLabel: '计息周期',
              valueField : 'code',
              displayField : 'name',
              typeAhead : true,
              width: 120,
              mode:'remote', 
              emptyText : '请选择计息周期...',
              editable: false,
              triggerAction : 'all',
              selectOnFocus : true,
              hiddenName:'interestCycle',
              
		}
        	   
        	   ]} 
]}, 

{
	layout:"column",
items:[{
	columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [
        	   { 
				xtype: 'textfield', 
				id: 'interestRate',
				name: 'interestRate', 
				fieldLabel: '利率值', 
				anchor: '98%' 
				}
           ]},
           
           { columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [
        	 {
		xtype: 'combo', 
		 id : "pinterestType",
              store :addPInterestTypeStore,
              fieldLabel: '罚息利率类型',
              valueField : 'code',
              displayField : 'name',
              typeAhead : true,
              width: 120,
              mode:'remote', 
              emptyText : '请选择罚息利率类型...',
              editable: false,
              triggerAction : 'all',
              selectOnFocus : true,
              hiddenName:'pinterestType',
              
		}
        	   
        	   ]} 
]},

{
	layout:"column",
items:[{
	columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [
        	   { 
				xtype: 'textfield', 
				id: 'interestRateCode',
				name: 'interestRateCode', 
				fieldLabel: '利率代码', 
				anchor: '98%' 
				}
           ]},
           
           { columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [
        	{ 
				xtype: 'textfield', 
				id: 'pinterestRateCode',
				name: 'pinterestRateCode', 
				fieldLabel: '罚息利率代码', 
				blankText: '罚息利率代码不能为空', 
				anchor: '98%' 
				}
        	   ]} 
]},

{ layout : "form",
           items : [{
xtype: 'hidden', 
 id : "debtNo",
              fieldLabel: '债项ID',
              allowBlank: true
}]},

{ layout : "form",
           items : [{
xtype: 'hidden', 
 id : "custId",
              fieldLabel: '债项ID',
              allowBlank: true
}]},

{ layout : "form",
           items : [{
xtype: 'hidden', 
 id : "custName",
              fieldLabel: '债项ID',
              allowBlank: true
}]}
] 
}]
});

//新增用户窗口 
addWin = new Ext.Window({ 
id: 'addCustomerWin', 
title: '新增债项', 
width: 670, 
height: 360, 
//背景遮罩 
modal: true, 
//重置大小 
resizable: false, 
//当关闭按钮被点击时执行的动作 
closeAction: 'hide', 
plain: true, 
buttonAlign: 'right', 
items:addForm, 
buttons: 
[  
{ text: '提交', id: 'btnSubmit' } ,
{ text: '重置', handler: function(){ Ext.getCmp('addCustomerForm').form.reset();}},
{ text: '关闭', handler: function() { Ext.getCmp('addCustomerWin').hide(); } }

] 
});

function addRoleFunction() {

	
	var addForm = Ext.getCmp("addCustomerForm");

if(addForm.form.isValid()){
	 addForm.form.doAction('submit', { 

                    url : 'obligation/addDebt.do', 
 
                    method : 'post', 
 
                       
                       success : function() { 

                       
                       Ext.getCmp('myCustomers').store.reload();
                       Ext.Msg.alert('提示', '保存成功！'); 
 
                       }, 
                        failure : function(form, action) { 

                           Ext.Msg.alert('提示', '保存未成功！'); 
 
                        } 
                    });  
                    }
 }

//添加按钮单击事件 
function btnAddClick() { 
//Ext.getCmp('addCustomerForm').form.reset(); 
//Ext.getCmp("addCustomerWin").setTitle('新增债项'); 
//Ext.getCmp("addCustomerWin").buttons[0].handler = addRoleFunction; 
//Ext.getCmp("addCustomerWin").show(); 
	//alert("新增");
	window.location.href = "obligation/addDebtDetail.do";
}; 

//新增按钮 
var addUserBtn = Ext.getCmp('btnAdd'); 
addUserBtn.on('click', btnAddClick); 

//删除
function deleteCustomer(){
var grid = Ext.getCmp('myCustomers'); 
if (grid.getSelectionModel().getSelections()[0] == undefined) { 
Ext.Msg.alert("提示", "请选中要删除的项"); 
} 
else{
Ext.MessageBox.confirm('提示', '确实要删除所选中项吗？', deleteConfirm);
}
}

function deleteConfirm(btn){
if (btn == 'yes') {
	Ext.Ajax.request({
		  url:'obligation/checkDeleteDebt.do',
		  params : {debtNo : grid.getSelectionModel().getSelections()[0].data.debtNo},
		  method:'POST',
		  success:function(resp,opts){
			  var respText = Ext.util.JSON.decode(resp.responseText);
			  if(respText.success){
				  Ext.Ajax.request({
					  url:'obligation/deleteDebt.do',
					  params : {debtNo : grid.getSelectionModel().getSelections()[0].data.debtNo},
					  method:'POST',
					  success:function(){
					   Ext.MessageBox.alert("提示","删除成功！");
					   Ext.getCmp('myCustomers').store.reload(); 
					  },failure:function(){
					    Ext.MessageBox.alert("提示", "删除失败！"); 
					  }
					});
			  }
			  else{
				  Ext.MessageBox.alert("提示", "该债项不可删除！"); 
			  }
		  },failure:function(){
			  Ext.MessageBox.alert("提示", "删除失败！"); 
		  }
		});
	
 
}
}


function checkMyCustomer(){
if (grid.getSelectionModel().getSelections()[0] == undefined) { 
Ext.Msg.alert("提示", "请选中要查看的项！"); 
}
else{
		var debtNo = grid.getSelectionModel().getSelections()[0].data.debtNo;
	
	Ext.getCmp('addCustomerForm').form.reset(); 
	Ext.getCmp("addCustomerWin").buttons[0].handler = editAddressFunction; 
	
           Ext.Ajax.request({
url:'obligation/getDebt.do',
params : { debtNo: grid.getSelectionModel().getSelections()[0].data.debtNo},
method:'POST',
   success:function(response){
 var addJSON = Ext.util.JSON.decode(response.responseText);

 	Ext.getCmp("addCustomerWin").show();
  	Ext.getCmp("debtNo").setValue(addJSON.debtNo);  
	Ext.getCmp("custId").setValue(addJSON.custId);
  	Ext.getCmp("custName").setValue(addJSON.custName);
  	Ext.getCmp("debtDesc").setValue(addJSON.debtDesc);
  	Ext.getCmp("debtType").setValue(addJSON.debtType);
   	Ext.getCmp("debtSum").setValue(addJSON.debtSum);
   	Ext.getCmp("debtBalance").setValue(addJSON.debtBalance);
   	Ext.getCmp("currency").setValue(addJSON.currency);
   	Ext.getCmp("startDate").setValue(addJSON.startDate);
   	Ext.getCmp("endDate").setValue(addJSON.endDate);
   	Ext.getCmp("businessKind").setValue(addJSON.businessKind);
	Ext.getCmp("guarantyKind").setValue(addJSON.guarantyKind);
	Ext.getCmp("groupLoanType").setValue(addJSON.groupLoanType);
	Ext.getCmp("interestSum").setValue(addJSON.interestSum);
  	Ext.getCmp("interestCycle").setValue(addJSON.interestCycle);
  	Ext.getCmp("interestRate").setValue(addJSON.interestRate);
  	Ext.getCmp("pinterestType").setValue(addJSON.interestRate);
  	Ext.getCmp("interestRateCode").setValue(addJSON.interestRateCode);
  	Ext.getCmp("pinterestRateCode").setValue(addJSON.pinterestRateCode);
  	 
	
  },failure:function(){
    Ext.MessageBox.alert("请求异常！");
  }
}); 
}
}


//编辑
function editAddressFunction(){
	
	var addForm = Ext.getCmp("addCustomerForm");
if(addForm.form.isValid()){
	 addForm.form.doAction('submit', { 

                    url : 'obligation/editDebt.do', 
 
                    method : 'post', 
 
                       
                       success : function() { 

                       
                       Ext.getCmp('myCustomers').store.reload();
                       Ext.Msg.alert('提示', '保存成功！'); 
 
                       }, 
                        failure : function(form, action) { 

                           Ext.Msg.alert('提示', '保存失败！'); 
 
                        } 
                    }); 
                    }
}

function viewDetail(){
	if (grid.getSelectionModel().getSelections()[0] == undefined) { 
		Ext.Msg.alert("提示", "请选中要操作的内容！"); 
	}else{
		var selectedDebtNo = grid.getSelectionModel().getSelections()[0].data.debtNo;

             Ext.Ajax.request({
			  url:'checkRequest.do',
			  method:'GET',
			  success:function(){
			  window.location.href="obligation/viewDebtDetail.do?debtNo="+selectedDebtNo;
			  },failure:function(){
			    Ext.MessageBox.alert("请求异常！");
			  }
			})
	}
}

function rowdblclickFn(grid, rowIndex, e){//双击事件  
             var row = grid.getStore().getAt(rowIndex); 
             var selectedDebtNo = row.get("debtNo");
             Ext.Ajax.request({
  url:'checkRequest.do',
  method:'GET',
  success:function(){
  window.location.href="obligation/viewDebtDetail.do?debtNo="+selectedDebtNo;
  },failure:function(){
    Ext.MessageBox.alert("请求异常！");
  }
});  
           
        }   
grid.addListener('rowdblclick',rowdblclickFn);
        
        
}); 